<template>
  <div class="container-box" id="container-box">
    <el-row>
      <el-col :xl="12" :lg="14" :md="16" :sm="16" :xs="18">
        <el-form :model="roleForm" status-icon :rules="rules" ref="roleForm" label-width="100px">
          <el-form-item label="账号：">
            <el-input v-model="roleForm.account" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="密码：" prop="password">
            <el-input type="password" @blur="passwordValidate" v-model="roleForm.password" clearable placeholder="请输入内容"></el-input>
          </el-form-item>
          <el-form-item label="状态：" prop="active">
            <el-select v-model="roleForm.active" placeholder="请选择角色状态">
              <el-option label="可用" value="1"></el-option>
              <el-option label="禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('roleForm')">立即更新</el-button>
            <el-button @click="resetForm('roleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { timestampToDate } from "../../assets/js/utils";

export default {
  data() {
    return {
      roleForm: {
        role_id: "",
        role_name: "",
        account: "",
        role: "",
        mobile: "",
        password: "",
        active: ""
      },
      roleData: {},
      params: {
        "access-token": window.localStorage.getItem("token")
      },
      rules: {
        role_name: [
          { required: true, message: "请输入姓名" },
          { min: 2, max: 20, message: "长度在 2 到 20 个字符" }
        ],
        role: [
          { required: true, message: "请选择角色权限", trigger: "change" }
        ],
        mobile: [
          { required: true, message: "请输入手机号码" },
          {
            validator: function(rule, value, callback) {
              if (/^1[34578]\d{9}$/.test(value) == false) {
                callback(new Error("请输入正确的手机号"));
              } else {
                callback();
              }
            }
          }
        ],
        password: [
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符"
          },
          {
            pattern: /^(\w){6,20}$/,
            message: "只能输入6-20个字母、数字、下划线"
          }
        ],
        active: [{ required: true, message: "请选择状态", trigger: "change" }]
      }
    };
  },
  created: function() {
    this.getRole();
  },
  methods: {
    passwordValidate() {
      this.$nextTick(() => {
        if (this.roleForm.password.length < 1)
          this.$refs.roleForm.clearValidate("password");
      });
    },
    getRole: function() {
      let role_id = this.$route.params.id;

      this.axios.get("roles/" + role_id, { params: this.params }).then(res => {
        let data = res.data.data.role;

        this.roleForm = {
          role_id: data.role_id,
          role_name: data.role_name,
          account: data.account,
          role: data.role,
          mobile: data.mobile,
          password: "",
          active: data.active
        };

        this.roleData = JSON.parse(JSON.stringify(this.roleForm));

        this.resetForm("roleForm");
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let token = window.localStorage.getItem("token");
          this.axios
            .patch(
              "roles/" +
                this.roleForm.role_id +
                "?access-token=" +
                window.localStorage.getItem("token"),
              this.roleForm
            )
            .then(res => {
              if (res.data.code === "1000") {
                this.$message({
                  type: "success",
                  message: "更新成功!"
                });

                this.$refs.roleForm.clearValidate();
              } else {
                this.$message({
                  type: "error",
                  message: res.data.message
                });
              }
            });
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.roleForm = JSON.parse(JSON.stringify(this.roleData));
    }
  }
};
</script>

<style lang="scss" scoped>
</style>
